/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DSB.View;

import DSB.Controller.KaartController;
import DSB.Controller.ToeristController;
import DSB.Model.Kaart.Enums;
import DSB.Utilities.Utilities;
import java.io.File;
import java.util.Observable;
import java.util.Observer;

/**
 *
 * @author Gebruiker
 */
public class Instellingenscherm extends javax.swing.JFrame implements Observer {
    Observable observable;
    private static KaartController kaartController;
    private static ToeristController toeristController;
    private Hoofdscherm _scherm;
    


    /**
     * Creates new form Instellingenscherm
     * @param scherm 
     */
    public Instellingenscherm(Hoofdscherm scherm) {
        initComponents();
        _scherm = scherm;
        kaartController = KaartController.getInstance();
        toeristController = ToeristController.getInstance();
        this.observable = ToeristController.getInstance();
        observable.addObserver(this);
        LoadRegions();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jInternalFrame1 = new javax.swing.JInternalFrame();
        RegionLabel = new javax.swing.JLabel();
        RegionDropDown = new javax.swing.JComboBox();
        Routelabel = new javax.swing.JLabel();
        RouteDropDown = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        Instellen = new javax.swing.JButton();
        ChangeLang = new javax.swing.JComboBox();
        ChangeMeetEenheid = new javax.swing.JComboBox();

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jInternalFrame1.setVisible(true);

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        setTitle("Opties");
        setAlwaysOnTop(true);
        setBounds(new java.awt.Rectangle(75, 110, 0, 0));
        setName("OptionsScreen"); // NOI18N

        RegionLabel.setText("Kies een plaats ");
        RegionLabel.setName("RegionLabel"); // NOI18N

        RegionDropDown.setName("SelectRegion");
        RegionDropDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegionDropDownActionPerformed(evt);
            }
        });

        Routelabel.setText("Kies een route ");
        Routelabel.setName("RouteLabel"); // NOI18N

        RouteDropDown.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        Instellen.setText("Verder");
        Instellen.setName("Instellen");
        Instellen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InstellenActionPerformed(evt);
            }
        });

        ChangeLang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nederlands", "English", "Deutsch" }));
        ChangeLang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChangeLangActionPerformed(evt);
            }
        });

        ChangeMeetEenheid.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Metrisch", "Feet" }));
        ChangeMeetEenheid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChangeMeetEenheidActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(270, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(Instellen))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(Routelabel)
                                    .addComponent(RegionLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(RegionDropDown, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(RouteDropDown, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(ChangeMeetEenheid, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(ChangeLang, 0, 125, Short.MAX_VALUE))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(ChangeLang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ChangeMeetEenheid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RegionLabel)
                    .addComponent(RegionDropDown, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Routelabel)
                    .addComponent(RouteDropDown, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(60, 60, 60)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addComponent(Instellen)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LoadRegions()
    {
        File[] regions = kaartController.GetRegions();
        
        if(RegionDropDown.getItemCount() > 0)
        {
            RegionDropDown.removeAllItems();
        }
        
        if(regions != null && regions.length > 0)
        { 
            String name = RegionDropDown.getName();
            String selectItem = toeristController.GetResource(name);
            if(selectItem != null)
            {
                RegionDropDown.addItem(selectItem);
            }
            for (int i=0; i < regions.length; i++)
            {
                RegionDropDown.addItem(regions[i].getName());
            }
        }
        else
        {
            RegionDropDown.addItem("Geen kaarten gevonden");
        }
    }
    private void RegionDropDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RegionDropDownActionPerformed
        if(RegionDropDown.getSelectedItem() != null)
        {
            File[] routes = kaartController.GetRoutes(RegionDropDown.getSelectedItem().toString());
            if(RouteDropDown.getItemCount() > 0)
            {
                RouteDropDown.removeAllItems();
            }

            if(routes != null && routes.length > 0)
            {
                for(int i=0; i< routes.length; i++)
                {
                    RouteDropDown.addItem(routes[i].getName());
                }
                RouteDropDown.setEnabled(true);
            }
            else
            {
                RouteDropDown.setEnabled(false);
            }
        }
    }//GEN-LAST:event_RegionDropDownActionPerformed

    private void InstellenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InstellenActionPerformed
        if(RegionDropDown.getSelectedItem() != null && RouteDropDown.getSelectedItem() != null &&
                !kaartController.getSelectedRegion().equals(RegionDropDown.getSelectedItem().toString()) &&
                        !kaartController.getSelectedRoute().equals(RouteDropDown.getSelectedItem().toString()))
        {
            kaartController.LoadMap(RegionDropDown.getSelectedItem().toString(), RouteDropDown.getSelectedItem().toString());
            //toeristController.SaveMapSettings(resizeImage(0.08, kaartController.getMap(RegionDropDown.getSelectedItem().toString(), RouteDropDown.getSelectedItem().toString())));
            _scherm.DrawMap();
        }
        this.setVisible(false);
    }//GEN-LAST:event_InstellenActionPerformed

    private void ChangeLangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangeLangActionPerformed
        toeristController.ChangeLanguage(ChangeLang.getSelectedIndex());
    }//GEN-LAST:event_ChangeLangActionPerformed

    private void ChangeMeetEenheidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangeMeetEenheidActionPerformed
        toeristController.ChangeMeetEenheid(ChangeMeetEenheid.getSelectedIndex());
    }//GEN-LAST:event_ChangeMeetEenheidActionPerformed

    
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ChangeLang;
    private javax.swing.JComboBox ChangeMeetEenheid;
    private javax.swing.JButton Instellen;
    private javax.swing.JComboBox RegionDropDown;
    private javax.swing.JLabel RegionLabel;
    private javax.swing.JComboBox RouteDropDown;
    private javax.swing.JLabel Routelabel;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void update(Observable obs, Object o1) {
        if (obs instanceof ToeristController) {
            ToeristController toeristController = (ToeristController) obs;

            Enums.ItemChanged changed = toeristController.getChange();

            switch (changed) {
                case LANGUAGE:
                    Utilities.TranslateScreen(this);
                    LoadRegions();
                    break;
            }
        }
    }
}
